﻿using System;
using System.Collections.Generic;
using System.Text;
using VietKinsNutrition.Data;
using System.Windows.Forms;
using System.Data;
using VietKinsNutrition.Common;
using DevExpress.XtraEditors;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_CapnhatthucphamBLL
    {

        ClsTblDm_CapnhatthucphamDAL ClsTblDm_Capnhatthucphamdal = new ClsTblDm_CapnhatthucphamDAL();

        #region Biến
        private int _idThucAn;
        private int _i;
        private TextEdit[] _temp;
        private int _tenThucPham;
        private int _donViTinh;


        public int tenThucPham
        {
            get { return _tenThucPham; }
        }
        public int donViTinh
        {
            get { return _donViTinh; }
        }
        public TextEdit[] temp
        {
            set { _temp = value; }
        }
        public int i
        {
            set { _i = value; }
        }
        public int IdThucAn
        {
            set { _idThucAn = value; }
        }

        #endregion


        public void FillComboBox(DevExpress.XtraEditors.LookUpEdit lueNhomTP, DevExpress.XtraEditors.ComboBoxEdit CbDVT)
        {
            try
            {
                lueNhomTP.Properties.DataSource = ClsTblDm_Nhomthucpham_ThucanDAL.GetTable();
                lueNhomTP.Properties.DisplayMember = "Tennhom";
                lueNhomTP.Properties.ValueMember = "Id";
                lueNhomTP.Properties.PopulateColumns();
                lueNhomTP.Properties.Columns["Id"].Visible = false;
                lueNhomTP.Properties.Columns["STT"].Width = 15;
                lueNhomTP.Properties.NullText = "Tất cả thực phẩm";
            }
            catch { }
            try
            {
                DataTable dTable = ClsTblDm_DonvitinhDAL.GetTable();
                UserComboBoxItem.SetItem(CbDVT, dTable, "Id", "Tendonvitinh");
            }
            catch { }
        }

        public void Creats(TextEdit[] Txt, CheckEdit[] Check)
        {
            for (int i = 0; i < Txt.Length; i++)
            {
                Txt[i].Text = "0";
            }
            for (int i = 0; i < Check.Length; i++)
            {
                Check[i].Checked = false;
            }
        }

        public bool DataBase(int Id, int Option, string Ten, TextEdit name, int[] Combo, TextEdit[] txt, CheckEdit[] check, TextEdit calo, DateEdit pic)
        {
            bool ok = false;
            switch (Option)
            {
                case 1:
                    {
                        if (CheckInput(txt, calo) == true)
                        {
                            if (CheckName("", name, 1))
                            {
                                _temp = new TextEdit[txt.Length - 2];
                                bool[] nguyenLieu = new bool[] { check[0].Checked, check[1].Checked };
                                int[] Nhom = new int[] { Combo[0], int.Parse(txt[0].Text.ToString()), int.Parse(txt[1].Text.ToString()), Combo[1]};
                                ClsTblDm_Capnhatthucphamdal.InsertData(Nhom, name.Text, float.Parse(calo.Text.ToString()), nguyenLieu, Convert.ToDateTime(pic.Text));
                                _i = 0;
                                for (int j = 2; j < txt.Length; j++)
                                {
                                    _temp[_i] = txt[j];
                                    _i = _i + 1;
                                }
                                _idThucAn = this.GetID(name.Text.Trim().ToString());
                                ClsTblDm_Capnhatthucphamdal.InsertTPHH("PrcTblDm_Thanhphanhoahoc_Insert", _temp, _idThucAn);
                                ok = true;
                            }
                        }
                        break;
                    }
                case 2:
                    {
                        if (CheckInput(txt, calo))
                        {
                            if (CheckName(Ten, name, 2))
                            {
                                _temp = new TextEdit[txt.Length - 2];
                                bool[] nguyenLieu = new bool[] { check[0].Checked, check[1].Checked };
                                int[] Nhom = new int[] { Combo[0], int.Parse(txt[0].Text.ToString()), int.Parse(txt[1].Text.ToString()), Combo[1] };
                                ClsTblDm_Capnhatthucphamdal.InsertData(Id, "PrcTblDm_Capnhatthucpham_Update", Nhom, name.Text, int.Parse(calo.Text), nguyenLieu, Convert.ToDateTime(pic.Text));
                                _i = 0;
                                for (int j = 2; j < txt.Length; j++)
                                {
                                    _temp[_i] = txt[j];
                                    _i = _i + 1;
                                }
                                ClsTblDm_Capnhatthucphamdal.InsertTPHH("PrcTblDm_Thanhphanhoahoc_Update", _temp, Id);
                                ok = true;
                            }
                        }
                        break;
                    }
            }
            return ok;
        }

        public void InsrttData(int[] Nhom, string tenThucPham, float calo, bool[] nguyenLieu, DateTime nam)
        {
            ClsTblDm_Capnhatthucphamdal.InsertData(Nhom, tenThucPham, calo, nguyenLieu, nam);
        }
        private int GetID(string Value)
        {
            foreach (DataRow dr in ClsTblDm_Capnhatthucphamdal.GetbyName("@TenThucAn", Value).Rows)
                return int.Parse(dr["Id"].ToString());
            return 0;
        }
        public bool CheckInput(TextEdit[] Txt, TextEdit Calo)
        {
            float k; float m;
            for (int i = 0; i < Txt.Length; i++)
            {
                if (float.TryParse(Txt[i].Text.Trim().ToString(), out k) == false)
                {
                    XtraMessageBox.Show(" Thành phần " + Txt[i].Name.Remove(0, 3) + "  không đúng định dạng dữ liệu ", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Txt[i].Focus();
                    return false;
                }
            }
            if (float.TryParse(Calo.Text.Trim().ToString(), out m) == false)
            {
                XtraMessageBox.Show("Thành phần calo không đúng định dạng dữ liệu không đúng ", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Calo.Focus();
                return false;
            }
            return true;
        }
        public bool CheckName(string Ten, TextEdit Name, int Option)
        {
            if (Option == 2)
            {
                if (Ten == Name.Text.Trim())
                {
                    return true;
                }
            }
            if (ClsTblDm_Capnhatthucphamdal.GetbyName("@TenThucAn", Name.Text.Trim()).Rows.Count > 0)
            {
                MessageBox.Show("Tên thực phẩm " + Name.Text.Trim() + " đã tồn tại trên cơ sở dữ liệu ", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Name.Focus();
                return false;
            }
            if (Name.Text.Trim() == "")
            {
                MessageBox.Show("Tên thực phẩm không được nhập trống ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
        public void Display(int ID, TextEdit[] txt, TextEdit calo, LookUpEdit lueNhomTP, ComboBoxEdit combo, CheckEdit[] check, DateEdit picker)
        {
            DataTable dt;
            QueryString.PRCTBLDM_THANHPHANDINHDUONGTHUCAN_SELECTALL = "Select * from vwThucpham where id=?";
            dt = ClsTblDm_Capnhatthucphamdal.GetTable(QueryString.PRCTBLDM_THANHPHANDINHDUONGTHUCAN_SELECTALL, "@id", ID.ToString());
        
            foreach (DataRow dr in dt.Rows)
            {
                txt[0].Text = dr["Tenthucan"].ToString();
                txt[1].Text = dr["Tylequydoi"].ToString();
                txt[2].Text = dr["Tylethai"].ToString();
                _donViTinh = int.Parse(dr["dvtID"].ToString());
                _tenThucPham = int.Parse(dr["Nhom"].ToString());
                UserComboBoxItem.SelectItem(combo, dr["dvtID"].ToString());
                lueNhomTP.EditValue = _tenThucPham;
                calo.Text = dr["Calo"].ToString();
                if (dr["Nguyenlieu"].ToString() == "")
                { check[0].Checked = false; }
                else { check[0].Checked = bool.Parse(dr["Nguyenlieu"].ToString()); }
                if (dr["Nguondongvat"].ToString().Trim() == "")
                { check[1].Checked = false; }
                else { check[1].Checked = bool.Parse(dr["Nguondongvat"].ToString()); }
                if (dr["Nam"].ToString() == "")
                { picker.Text = DateTime.Now.ToString("dd/MM/yy"); }
                else { picker.EditValue = Convert.ToDateTime(dr["Nam"].ToString()); }
                txt[3].Text = dr["Tphh_Nuoc"].ToString();
                txt[4].Text = dr["Tphh_Protid"].ToString();
                txt[5].Text = dr["Tphh_Lipid"].ToString();
                txt[6].Text = dr["Tphh_GlucId"].ToString();
                txt[7].Text = dr["Tphh_Cellulose"].ToString();
                txt[8].Text = dr["Tphh_Tro"].ToString();
                txt[9].Text = dr["Tpmk_Calci"].ToString();
                txt[10].Text = dr["Tpmk_Photpho"].ToString();
                txt[11].Text = dr["Tpmk_Fe"].ToString();
                txt[12].Text = dr["Tpvitamin_Caroten"].ToString();
                txt[13].Text = dr["Tpvitamin_VitaminA"].ToString();
                txt[16].Text = dr["Tpvitamin_PP"].ToString();
                txt[17].Text = dr["Tpvitamin_VitaminC"].ToString();
                txt[18].Text = dr["Tpmk_Natri"].ToString();
                txt[19].Text = dr["Tpmk_Axitfomic"].ToString();
                txt[20].Text = dr["Tpmk_Kali"].ToString();
            }
        }

        public bool CheckCombo(LookUpEdit lue, ComboBoxEdit combo)
        { 
            if(lue.EditValue == null || lue.EditValue.ToString() == "")
            {
                XtraMessageBox.Show("Xin lựa chọn nhóm thực phẩm! ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lue.Focus();
                return false;
            }
            else if(combo.EditValue == null || combo.EditValue.ToString() == "*Lựa chọn*")
            {
                XtraMessageBox.Show("Xin lựa chọn đơn vị thực phẩm! ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lue.Focus();
                return false;
            }
            return true;
        }
    }
}
